import { ListBase } from 'src/components/list_base_abs';
import { Component, Emit, Prop, Provide, Watch } from 'vue-property-decorator'

import { WebVisitConst } from "src/consts/web_visit_const";
import { auditManageConst } from "src/consts/audit_manage_const";
import { updateSelection } from 'src/utils/list_utils';
import OperatorHandler from 'src/views/web_visit/operator_handler';
import ListDemoHandler from 'src/views/web_visit/list_handler';
@Component({})
export default class WebVisit extends ListBase {
    operatorData: OperatorHandler;
    listHandler: ListDemoHandler;
    mainData: any
    queryData: any;
    activeRouteItemRoute: string
    isNow: Boolean

    initData(that: WebVisit) {
        that.myState = this.$store.state.webVisit;
        that.selection = [];
        that.operatorData = new OperatorHandler();
        that.listHandler = new ListDemoHandler();
        that.operatorData.init(this);
        that.mainData = {};
        that.isNow = false;
        that.queryData = {};
    }

    getListData(): void {
        let sendData: any = {}
        sendData["curr_page"] = this.myState.curr_page
        sendData["page_size"] = this.myState.page_size
        sendData.id = getUserInfo().userId
        WLDispatch(WebVisitConst.DispatchWebVList, sendData);
    }

    get _activeRouteItemRoute(): string {
        return this.$route.path
    }

    @Watch('_activeRouteItemRoute', { immediate: true })
    onRouteChanged(val: any) {
        if (val == "/web_visit") {
            this.getListData()
        }
    }

    doReturn(): void {
        let sendData: any = {}
        sendData["curr_page"] = 1
        sendData["page_size"] = this.myState.page_size
        sendData.id = getUserInfo().userId
        WLDispatch(WebVisitConst.DispatchWebVList, sendData);
        this.isNow = false
    }

    setVal(data: string): String {
        return getValueByKey("web_status", data)
    }

    mounted(): void {
        this.getListData();
    }

    doQuery(isAdv: Boolean): void {
        var obj: any = {}
        for (let key in this.mainData) {
            if (this.mainData[key]) {
                obj[key] = this.mainData[key]
                if (key == "create_name") {
                    obj[key] = this.mainData[key].name
                } else if (key == "name") {
                    obj[key] = this.mainData[key].web_name
                }
            }
        }

        this.queryData = obj
        obj["curr_page"] = 1
        obj["page_size"] = 15
        WLDispatch(WebVisitConst.DispatchWebVNowList, obj);

        this.isNow = true
    }

    sizeChange(size: number): void {
        let sendData: any = this.queryData
        sendData["curr_page"] = this.myState.curr_page
        sendData["page_size"] = size
        sendData.id = getUserInfo().userId

        if (this.isNow) {
            WLDispatch(WebVisitConst.DispatchWebVNowList, sendData);
        } else {
            WLDispatch(WebVisitConst.DispatchWebVList, sendData);
        }
    }

    pageChange(page: number): void {
        let sendData: any = this.queryData
        sendData["curr_page"] = page
        sendData["page_size"] = this.myState.page_size
        sendData.id = getUserInfo().userId
        if (this.isNow) {
            WLDispatch(WebVisitConst.DispatchWebVNowList, sendData);
        } else {
            WLDispatch(WebVisitConst.DispatchWebVList, sendData);
        }
    }

    handleSelectionChange(list: any[]): void {
        updateSelection(this.selection, list);
        this.handleBtnShow(this.selection)
        console.log("this selection", this.selection, list);
    }

    handleBtnShow(data: any): void {
    }

    rowClassName(data: any): string {
        if (data.row.selected) {
            return 'selected_row';
        }
        return '';
    }

    doNew(): void {
        if (this.operatorData && this.operatorData.doNew) {
            this.operatorData.doNew();
        } else {
            this.$message.warning("todo doNew 方法");
        }
    }

    rowEdit(ind: number, row: DemoData): void {
        if (this.listHandler && this.listHandler.rowEdit) {
            this.listHandler.rowEdit(ind, row);
        } else {
            console.warn("todo 初始化和实现行内容编辑");
        }
    }
}